﻿using System;
using System.Data;
using System.Drawing;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using Elkomp;

namespace TxtNumeric {

	public class TxtNum : TextBox {

		protected override void OnGotFocus( EventArgs e ) {
			base.BackColor = Color.LightGreen;
			base.OnGotFocus( e );
		}

		protected override void OnEnter( EventArgs e ) {
			if ( MakedonskiJazik ) {
				InputLanguage.CurrentInputLanguage = InputLanguage.InstalledInputLanguages[1];
			}

			base.OnEnter( e );
		}

		protected override void OnLostFocus( EventArgs e ) {
			base.OnLostFocus( e );
		}

		public string formToOpen { set; get; }

		public string AssemblyStr { set; get; }

		public string ColumnName1 { set; get; }

		public string ColumnName2 { set; get; }

		public string ColumnName3 { set; get; }

		public string ColumnName4 { set; get; }

		public string ColumnName5 { set; get; }

		public string ColumnName6 { set; get; }

		public string ColumnName7 { set; get; }

		public string ret1 = "";
		public string ret2 = "";
		public string ret3 = "";
		public string ret4 = "";
		public string ret5 = "";
		public string ret6 = "";
		public string ret7 = "";

		public delegate void doneLoading();
		public event doneLoading DataRead;
		public bool read;

		public bool PopulateOtherFields { set; get; }

		public bool IsTextEmpty( String txt ) {
			return string.IsNullOrEmpty( txt );
		}

		public void OpenForm() {
			if ( string.IsNullOrEmpty( AssemblyStr ) && string.IsNullOrEmpty( formToOpen ) ) { return; }
			if ( AssemblyStr.Length > 1 && formToOpen.Length > 1 ) {
				System.Reflection.Assembly asm = System.Reflection.Assembly.Load( AssemblyStr );
				Type t = asm.CreateInstance( formToOpen, true ).GetType();
				FormaBrowse frm = ( FormaBrowse )Activator.CreateInstance( t, new object[] { true } );
				frm.AcceptButton = frm.btnOk;
				frm.btnOk.Focus();
				frm.ShowDialog();
				DataRow dtr;
				dtr = frm.dr;
				frm.Dispose();

				if ( frm.PrifatiKlik == true ) {
					base.Text = dtr[ColumnName1].ToString();
					if ( PopulateOtherFields ) {
						try {
							if ( !IsTextEmpty( ColumnName1 ) ) { ret1 = dtr[ColumnName1].ToString(); }
							if ( !IsTextEmpty( ColumnName2 ) ) { ret2 = dtr[ColumnName2].ToString(); }
							if ( !IsTextEmpty( ColumnName3 ) ) { ret3 = dtr[ColumnName3].ToString(); }
							if ( !IsTextEmpty( ColumnName4 ) ) { ret4 = dtr[ColumnName4].ToString(); }
							if ( !IsTextEmpty( ColumnName5 ) ) { ret5 = dtr[ColumnName5].ToString(); }
							if ( !IsTextEmpty( ColumnName6 ) ) { ret6 = dtr[ColumnName6].ToString(); }
							if ( !IsTextEmpty( ColumnName7 ) ) { ret7 = dtr[ColumnName7].ToString(); }
						}
						catch ( Exception ex ) {
							MessageBox.Show( ex.ToString() );
						}
						this.DataRead.Invoke();
					}
					read = true;
				}
			}
		}

		public bool IsTextEmpty() {
			return string.IsNullOrEmpty( base.Text );
		}

		public bool C_OnlyNumbers { get; set; }

		public bool C_ShowRedOnLeaveIfEmpty { get; set; }

		public bool C_RegEx { get; set; }

		public bool MakedonskiJazik { get; set; }

		public string regularExpression { get; set; }

		private bool NumberEntered;

		private bool CheckIfNumericKey( Keys K, bool isDecimalPoint ) {
			if ( K == Keys.Back ) //backspace?
				return true;

			else if ( K == Keys.OemPeriod || K == Keys.Decimal )  //decimal point?
				return isDecimalPoint ? false : true;       //or: return !isDecimalPoint
			else if ( ( K >= Keys.D0 ) && ( K <= Keys.D9 ) )      //digit from top of keyboard?
				return true;
			else if ( ( K >= Keys.NumPad0 ) && ( K <= Keys.NumPad9 ) )    //digit from keypad?
				return true;
			else
				return false;   //no "numeric" key
		}

		protected override void OnLeave( EventArgs e ) {
			if ( IsTextEmpty() && C_ShowRedOnLeaveIfEmpty == true ) {
				base.BackColor = Color.Red;
			} else {
				base.BackColor = Color.Empty;
			}

			base.OnLeave( e );
		}

		protected override void OnKeyPress( KeyPressEventArgs e ) {
			base.BackColor = Color.LightGreen;
			if ( C_OnlyNumbers == true ) {
				if ( NumberEntered == false ) {
					e.Handled = true;
				}
			} else
				if ( C_RegEx == true && regularExpression.Length > 1 ) {
					if ( !Regex.IsMatch( base.Text, regularExpression ) && !Char.IsControl( e.KeyChar ) ) {
						e.Handled = true;
					}
				}

			base.OnKeyPress( e );
		}

		protected override void OnKeyDown( KeyEventArgs e ) {
			if ( e.KeyCode != Keys.F5 ) {
				NumberEntered = CheckIfNumericKey( e.KeyCode, false );
				base.OnKeyDown( e );
			} else {
				OpenForm();
			}
		}
	}
}